package de.Objects;

import java.awt.Font;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.Shape3D;
import javax.media.j3d.SpotLight;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.image.TextureLoader;

public class Spielbrett extends BranchGroup {

	TransformGroup tg1 = new TransformGroup();

	BranchGroup root = new BranchGroup();

	Appearance apperBase = new Appearance();
	Appearance apper2ndBase = new Appearance();
	Appearance apperWays = new Appearance();
	Appearance apperBubbles = new Appearance();
	Appearance apperTable = new Appearance();
	Appearance apperLight = new Appearance();

	float dim1 = 5.0f;
	float dim2 = dim1 - 1.0f;
	float thick = 0.2f;
	int primflags;

	public Spielbrett(String pl1, String pl2) {
		createAppereances();
		createText(pl1, pl2);
		addLight();
		createTable();
		createBase();
		createOuterWays();
		createMiddleWays();
		createInnerWays();
		createCrossWays();

		root.addChild(tg1);
		super.addChild(root);
	}

	public void addLight() {
		// Ein SpotLight wird erzeugt
		SpotLight lightS1 = new SpotLight();
		lightS1.setDirection(new Vector3f(0.0f, 0.0f, 0.0f));
		lightS1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
		lightS1.setPosition(0.0f, 40.0f, 0.0f);
		lightS1.setSpreadAngle(10.0f);
		lightS1.setInfluencingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f,
				0.0f), 30.0f));
		root.addChild(lightS1);

		// Ein PointLight wird erzeugt
		PointLight lightP1 = new PointLight();
		lightP1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
		lightP1.setPosition(0.0f, 10.0f, 0.0f);
		// lightP1.setAttenuation(new Point3f(1.0f, 0.0f, 0.0f));// Daempfung
		lightP1.setInfluencingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f,
				0.0f), 30.0f));
		root.addChild(lightP1);
	}

	public void createAppereances() {
		primflags = Primitive.GENERATE_NORMALS
				+ Primitive.GENERATE_TEXTURE_COORDS;
		Color3f ambient = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f emissive = new Color3f(0.3f, 0.3f, 0.3f);
		Color3f diffuse = new Color3f(0.2f, 0.2f, 0.2f);
		Color3f specular = new Color3f(0.8f, 0.8f, 0.8f);
		Material material = new Material(ambient, emissive, diffuse, specular,
				100.0f);
		material.setLightingEnable(true);

// ////////Table
		TextureLoader loaderTable = new TextureLoader("textures/wood_wall.jpg",
				null);
		ImageComponent2D imageTable = loaderTable.getImage();
		if (imageTable == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureTable = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageTable.getWidth(), imageTable.getHeight());
			textTureTable.setImage(0, imageTable);
			apperTable.setTexture(textTureTable);

			TextureAttributes texAttrTable = new TextureAttributes();
			texAttrTable.setTextureMode(TextureAttributes.MODULATE);
			apperTable.setTextureAttributes(texAttrTable);
		}

// ////////Base
		TextureLoader loaderBase = new TextureLoader("textures/Marmor_1.jpg",
				null);
		ImageComponent2D imageBase = loaderBase.getImage();
		if (imageBase == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureBase = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageBase.getWidth(), imageBase.getHeight());
			textTureBase.setImage(0, imageBase);
			apperBase.setTexture(textTureBase);
			TextureAttributes texAttrBase = new TextureAttributes();
			texAttrBase.setTextureMode(TextureAttributes.MODULATE);
			apperBase.setTextureAttributes(texAttrBase);
		}
		apperBase.setMaterial(material);

// //////// 2nd Base
		TextureLoader loader2ndBase = new TextureLoader("textures/marble.jpg",
				null);
		ImageComponent2D image2ndBase = loader2ndBase.getImage();
		if (image2ndBase == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTure2nBase = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, image2ndBase.getWidth(),
					image2ndBase.getHeight());
			textTure2nBase.setImage(0, image2ndBase);
			apper2ndBase.setTexture(textTure2nBase);
			TextureAttributes texAttr2ndBase = new TextureAttributes();
			texAttr2ndBase.setTextureMode(TextureAttributes.MODULATE);
			apper2ndBase.setTextureAttributes(texAttr2ndBase);
			apper2ndBase.setMaterial(material);

			// TransparencyAttributes t_attr = new
			// TransparencyAttributes(TransparencyAttributes.BLENDED,0.6f,
			// TransparencyAttributes.BLEND_SRC_ALPHA,
			// TransparencyAttributes.BLEND_ONE);
			// apper2ndBase.setTransparencyAttributes( t_attr );
		}

// //////// Ways
		TextureLoader loaderWays = new TextureLoader("textures/platte.jpg",
				null);
		ImageComponent2D imageWays = loaderWays.getImage();
		if (imageWays == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureWays = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageWays.getWidth(), imageWays.getHeight());
			textTureWays.setImage(0, imageWays);
			apperWays.setTexture(textTureWays);
			TextureAttributes texAttrWays = new TextureAttributes();
			texAttrWays.setTextureMode(TextureAttributes.MODULATE);
			apperWays.setTextureAttributes(texAttrWays);
			apperWays.setMaterial(material);
		}

// ////////Lamp
		TextureLoader loaderLamp = new TextureLoader("textures/world.jpg", null);
		ImageComponent2D imageLamp = loaderLamp.getImage();
		if (imageLamp == null) {
			System.out.println("failed loading texture");
		} else {
			Texture2D textTureLamp = new Texture2D(Texture.BASE_LEVEL,
					Texture.RGBA, imageLamp.getWidth(), imageLamp.getHeight());
			textTureLamp.setImage(0, imageLamp);
			apperLight.setTexture(textTureLamp);
			TextureAttributes texAttrLamp = new TextureAttributes();
			texAttrLamp.setTextureMode(TextureAttributes.MODULATE);
			apperLight.setTextureAttributes(texAttrLamp);
		}
	}

	public void createTable() {

		Box tischPlatte = new Box(dim1 + 2, thick, dim1 + 2, primflags,
				apperTable);
		Transform3D translationTisch = new Transform3D();
		translationTisch.setTranslation(new Vector3f(0.0f, -0.5f, 0.0f));
		TransformGroup tgTisch = new TransformGroup(translationTisch);
		root.addChild(tgTisch);
		tgTisch.addChild(tischPlatte);

		Box bein1 = new Box(0.5f, 5.0f, 0.5f, primflags, apperTable);
		Transform3D translationBein1 = new Transform3D();
		translationBein1.setTranslation(new Vector3f(5.5f, -5.2f, 5.5f));
		TransformGroup tgBein1 = new TransformGroup(translationBein1);
		root.addChild(tgBein1);
		tgBein1.addChild(bein1);

		Box bein2 = new Box(0.5f, 5.0f, 0.5f, primflags, apperTable);
		Transform3D translationBein2 = new Transform3D();
		translationBein2.setTranslation(new Vector3f(-5.5f, -5.2f, 5.5f));
		TransformGroup tgBein2 = new TransformGroup(translationBein2);
		root.addChild(tgBein2);
		tgBein2.addChild(bein2);

		Box bein3 = new Box(0.5f, 5.0f, 0.5f, primflags, apperTable);
		Transform3D translationBein3 = new Transform3D();
		translationBein3.setTranslation(new Vector3f(5.5f, -5.2f, -5.5f));
		TransformGroup tgBein3 = new TransformGroup(translationBein3);
		root.addChild(tgBein3);
		tgBein3.addChild(bein3);

		Box bein4 = new Box(0.5f, 5.0f, 0.5f, primflags, apperTable);
		Transform3D translationBein4 = new Transform3D();
		translationBein4.setTranslation(new Vector3f(-5.5f, -5.2f, -5.5f));
		TransformGroup tgBein4 = new TransformGroup(translationBein4);
		root.addChild(tgBein4);
		tgBein4.addChild(bein4);
	}

	public void createBase() {
		Box box = new Box(dim1 - 0.5f, thick, dim1 - 0.5f, primflags, apperBase);
		Box box2 = new Box(dim2, thick, dim2, primflags, apper2ndBase);
		Transform3D translation = new Transform3D();
		translation.setTranslation(new Vector3f(0.0f, 0.3f, 0.0f));
		TransformGroup tg2 = new TransformGroup(translation);

		root.addChild(tg2);
		tg2.addChild(box2);
		tg1.addChild(box);
	}

	public void createOuterWays() {
		Transform3D rotationX = new Transform3D();
		rotationX.rotX(Math.PI / 2.0);
		Transform3D rotationY = new Transform3D();
		rotationY.rotZ(Math.PI / 2.0);
		Transform3D translation = new Transform3D();
		translation.mul(rotationX);
		translation.setTranslation(new Vector3f(3.5f, 0.5f, 0.0f));
		TransformGroup tgWays = new TransformGroup(translation);
		root.addChild(tgWays);
		Cylinder c1 = new Cylinder(0.1f, dim1 + 2, primflags, apperWays);
		tgWays.addChild(c1);

		Transform3D translation2 = new Transform3D();
		translation2.mul(rotationX);
		translation2.setTranslation(new Vector3f(-3.5f, 0.5f, 0.0f));
		TransformGroup tgWays2 = new TransformGroup(translation2);
		root.addChild(tgWays2);
		Cylinder c2 = new Cylinder(0.1f, dim1 + 2, primflags, apperWays);
		tgWays2.addChild(c2);

		Transform3D translation3 = new Transform3D();
		rotationX.mul(rotationY);
		translation3.mul(rotationX);
		translation3.setTranslation(new Vector3f(0.0f, 0.5f, 3.5f));
		TransformGroup tgWays3 = new TransformGroup(translation3);
		root.addChild(tgWays3);
		Cylinder c3 = new Cylinder(0.1f, dim1 + 2, primflags, apperWays);
		tgWays3.addChild(c3);

		Transform3D translation4 = new Transform3D();
		translation4.mul(rotationX);
		translation4.setTranslation(new Vector3f(0.0f, 0.5f, -3.5f));
		TransformGroup tgWays4 = new TransformGroup(translation4);
		root.addChild(tgWays4);
		Cylinder c4 = new Cylinder(0.1f, dim1 + 2, primflags, apperWays);
		tgWays4.addChild(c4);
	}

	public void createMiddleWays() {
		Transform3D rotationX = new Transform3D();
		rotationX.rotX(Math.PI / 2.0);
		Transform3D rotationY = new Transform3D();
		rotationY.rotZ(Math.PI / 2.0);

		Transform3D translation = new Transform3D();
		translation.mul(rotationX);
		translation.setTranslation(new Vector3f(2.5f, 0.5f, 0.0f));
		TransformGroup tgWays = new TransformGroup(translation);
		root.addChild(tgWays);
		Cylinder c1 = new Cylinder(0.1f, dim1, primflags, apperWays);
		tgWays.addChild(c1);

		Transform3D translation2 = new Transform3D();
		translation2.mul(rotationX);
		translation2.setTranslation(new Vector3f(-2.5f, 0.5f, 0.0f));
		TransformGroup tgWays2 = new TransformGroup(translation2);
		root.addChild(tgWays2);
		Cylinder c2 = new Cylinder(0.1f, dim1, primflags, apperWays);
		tgWays2.addChild(c2);

		Transform3D translation3 = new Transform3D();
		rotationX.mul(rotationY);
		translation3.mul(rotationX);
		translation3.setTranslation(new Vector3f(0.0f, 0.5f, 2.5f));
		TransformGroup tgWays3 = new TransformGroup(translation3);
		root.addChild(tgWays3);
		Cylinder c3 = new Cylinder(0.1f, dim1, primflags, apperWays);
		tgWays3.addChild(c3);

		Transform3D translation4 = new Transform3D();
		translation4.mul(rotationX);
		translation4.setTranslation(new Vector3f(0.0f, 0.5f, -2.5f));
		TransformGroup tgWays4 = new TransformGroup(translation4);
		root.addChild(tgWays4);
		Cylinder c4 = new Cylinder(0.1f, dim1, primflags, apperWays);
		tgWays4.addChild(c4);
	}

	public void createInnerWays() {
		Transform3D rotationX = new Transform3D();
		rotationX.rotX(Math.PI / 2.0);
		Transform3D rotationY = new Transform3D();
		rotationY.rotZ(Math.PI / 2.0);

		Transform3D translation = new Transform3D();
		translation.mul(rotationX);
		translation.setTranslation(new Vector3f(1.5f, 0.5f, 0.0f));
		TransformGroup tgWays = new TransformGroup(translation);
		root.addChild(tgWays);
		Cylinder c1 = new Cylinder(0.1f, dim1 - 2, primflags, apperWays);
		tgWays.addChild(c1);

		Transform3D translation2 = new Transform3D();
		translation2.mul(rotationX);
		translation2.setTranslation(new Vector3f(-1.5f, 0.5f, 0.0f));
		TransformGroup tgWays2 = new TransformGroup(translation2);
		root.addChild(tgWays2);
		Cylinder c2 = new Cylinder(0.1f, dim1 - 2, primflags, apperWays);
		tgWays2.addChild(c2);

		Transform3D translation3 = new Transform3D();
		rotationX.mul(rotationY);
		translation3.mul(rotationX);
		translation3.setTranslation(new Vector3f(0.0f, 0.5f, 1.5f));
		TransformGroup tgWays3 = new TransformGroup(translation3);
		root.addChild(tgWays3);
		Cylinder c3 = new Cylinder(0.1f, dim1 - 2, primflags, apperWays);
		tgWays3.addChild(c3);

		Transform3D translation4 = new Transform3D();
		translation4.mul(rotationX);
		translation4.setTranslation(new Vector3f(0.0f, 0.5f, -1.5f));
		TransformGroup tgWays4 = new TransformGroup(translation4);
		root.addChild(tgWays4);
		Cylinder c4 = new Cylinder(0.1f, dim1 - 2, primflags, apperWays);
		tgWays4.addChild(c4);
	}

	public void createCrossWays() {
		Transform3D rotationX = new Transform3D();
		rotationX.rotX(Math.PI / 2.0);
		Transform3D rotationY = new Transform3D();
		rotationY.rotZ(Math.PI / 2.0);

		Transform3D translation = new Transform3D();
		translation.mul(rotationX);
		translation.setTranslation(new Vector3f(0.0f, 0.5f, 2.4f));
		TransformGroup tgWays = new TransformGroup(translation);
		root.addChild(tgWays);
		Cylinder c1 = new Cylinder(0.1f, (dim1 / 2) - 0.4f, primflags,
				apperWays);
		tgWays.addChild(c1);

		Transform3D translation2 = new Transform3D();
		translation2.mul(rotationX);
		translation2.setTranslation(new Vector3f(0.0f, 0.5f, -2.4f));
		TransformGroup tgWays2 = new TransformGroup(translation2);
		root.addChild(tgWays2);
		Cylinder c2 = new Cylinder(0.1f, (dim1 / 2) - 0.4f, primflags,
				apperWays);
		tgWays2.addChild(c2);

		Transform3D translation3 = new Transform3D();
		rotationX.mul(rotationY);
		translation3.mul(rotationX);
		translation3.setTranslation(new Vector3f(2.4f, 0.5f, 0.0f));
		TransformGroup tgWays3 = new TransformGroup(translation3);
		root.addChild(tgWays3);
		Cylinder c3 = new Cylinder(0.1f, (dim1 / 2) - 0.4f, primflags,
				apperWays);
		tgWays3.addChild(c3);

		Transform3D translation4 = new Transform3D();
		translation4.mul(rotationX);
		translation4.setTranslation(new Vector3f(-2.4f, 0.5f, 0.0f));
		TransformGroup tgWays4 = new TransformGroup(translation4);
		root.addChild(tgWays4);
		Cylinder c4 = new Cylinder(0.1f, (dim1 / 2) - 0.4f, primflags,
				apperWays);
		tgWays4.addChild(c4);
	}

	public void createText(String player1, String player2) {
// ////////// change view 2
		Transform3D translationText = new Transform3D();
		translationText.rotX(Math.PI / -3.5f);
		translationText.setTranslation(new Vector3f(5.2f, 0.3f, 6.0f));
		TransformGroup tgSWT = new TransformGroup(translationText);
		root.addChild(tgSWT);

		Transform3D translationText2 = new Transform3D();
		translationText2.rotX(Math.PI / -3.5f);
		translationText2.setTranslation(new Vector3f(5.5f, -0.1f, 6.4f));
		TransformGroup tgSWT2 = new TransformGroup(translationText2);
		root.addChild(tgSWT2);

		Color3f color = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f color3 = new Color3f(1.0f, 1.0f, 1.0f);
		Text2D textSwitch = new Text2D("SWITCH", color3, "SansSerif", 100,
				Font.BOLD);
		Text2D text2Switch = new Text2D("VIEW", color3, "SansSerif", 100,
				Font.BOLD);
		tgSWT.addChild(textSwitch);
		tgSWT2.addChild(text2Switch);

// ////////// change view 2
		Transform3D translationText44 = new Transform3D();
		translationText44.rotY(Math.PI / 1.01f);
		Transform3D translationText4 = new Transform3D();
		translationText4.rotX(Math.PI / 3.5f);
		translationText4.mul(translationText44);
		translationText4.setTranslation(new Vector3f(-5.2f, 0.3f, -6.0f));
		TransformGroup tgSWT4 = new TransformGroup(translationText4);
		root.addChild(tgSWT4);

		Transform3D translationText55 = new Transform3D();
		translationText55.rotY(Math.PI / 1.01f);
		Transform3D translationText5 = new Transform3D();
		translationText5.rotX(Math.PI / 3.5f);
		translationText5.mul(translationText55);
		translationText5.setTranslation(new Vector3f(-5.5f, -0.1f, -6.4f));
		TransformGroup tgSWT5 = new TransformGroup(translationText5);
		root.addChild(tgSWT5);

		Text2D textSwitch2 = new Text2D("SWITCH", color3, "SansSerif", 100,
				Font.BOLD);
		Text2D text2Switch2 = new Text2D("VIEW", color3, "SansSerif", 100,
				Font.BOLD);
		tgSWT4.addChild(textSwitch2);
		tgSWT5.addChild(text2Switch2);

// ////////// Player Name 1
		Transform3D translationPlayer1 = new Transform3D();
		translationPlayer1.rotX(Math.PI / -2.0f);
		translationPlayer1.setTranslation(new Vector3f(-4.5f, -0.2f, 7.1f));
		TransformGroup tgPlayer1 = new TransformGroup(translationPlayer1);
		root.addChild(tgPlayer1);
		Color3f color2 = new Color3f(1.0f, 1.0f, 1.0f);
		Text2D textPlayer1 = new Text2D(player1, color2, "SansSerif", 250,
				Font.BOLD);
		tgPlayer1.addChild(textPlayer1);

// //////////Player Name 2
		Transform3D translationPlayer2 = new Transform3D();
		translationPlayer2.rotX(Math.PI / -2.0f);
		Transform3D translationPlayer22 = new Transform3D();
		translationPlayer22.rotZ(Math.PI / 1.0f);
		translationPlayer2.mul(translationPlayer22);
		translationPlayer2.setTranslation(new Vector3f(4.5f, -0.2f, -7.1f));
		TransformGroup tgPlayer2 = new TransformGroup(translationPlayer2);
		root.addChild(tgPlayer2);
		Text2D textPlayer2 = new Text2D(player2, color2, "SansSerif", 250,
				Font.BOLD);
		tgPlayer2.addChild(textPlayer2);
	}
}